<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>委托(delegate) | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="c#学习笔记" />
  
  
  
  
  <meta name="description" content="委托(delegate)">
<meta name="keywords" content="c#学习笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="委托(delegate)">
<meta property="og:url" content="http://www.hais2.com/2015/05/20/c#-delegate/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="委托(delegate)">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp31.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp32.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp33.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp34.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp35.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp36.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp37.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp38.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp39.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp40.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp41.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp42.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp43.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp44.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp45.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp46.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp47.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp48.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp49.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp50.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp51.png">
<meta property="og:image" content="http://www.hais2.com/image/cSharp/cSharp52.png">
<meta property="og:updated_time" content="2017-08-02T15:22:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="委托(delegate)">
<meta name="twitter:description" content="委托(delegate)">
<meta name="twitter:image" content="http://www.hais2.com/image/cSharp/cSharp31.png">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-c#-delegate" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      委托(delegate)
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2015/05/20/c#-delegate/" class="article-date">
	  <time datetime="2015-05-20T08:47:52.000Z" itemprop="datePublished">May 20, 2015</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="什么是委托"><a href="#什么是委托" class="headerlink" title="什么是委托"></a>什么是委托</h3><p>　　<strong>委托</strong>是一个包含具有相同签名和返回值类型的有序方法列表。<br>　　■ 方法的列表称为<strong>调用列表</strong>(invocation list)<br>　　■ 当委托被调用时，它调用列表中的每一个方法。<br><img src="/image/cSharp/cSharp31.png" alt=""><a id="more"></a><br>　　包含单个方法的委托和C++的函数指针相似，然而，与函数指针不同的是，委托是面向对象的，并且是类型安全的(type-safe)的。<br>　　<br>　　<strong>“调用列表”中的方法</strong>：<br>　　由委托保存的方法可以来自任何类或者结构，只要他们同时匹配委托的如下两点：<br>　　■ 返回值<br>　　■ 签名(包括ref和out修饰符)<br>　　调用列表中的方法可以是实例方法或者静态方法。</p>
<h3 id="声明委托类型"><a href="#声明委托类型" class="headerlink" title="声明委托类型"></a>声明委托类型</h3><p>　　委托是一种数据类型，就好像类是类型一样。与类相同，委托类型必须在被用来创建变量以及类型的对象之前进行声明，才可以使用。<br><img src="/image/cSharp/cSharp32.png" alt=""><br>　　委托类型的声明看上去和方法的声明比较相似，有返回值类型和签名，<strong>返回值类型和签名指定了委托接受的方法的形式</strong>。<br>　　上图中的代码声明了<em>MyDel</em>类型的委托。声明指定了这种类型的委托只会接受没有返回值(void)并且有单个int参数的方法。<br><img src="/image/cSharp/cSharp33.png" alt=""></p>
<p>　　<strong>委托类型声明与方法声明的两个不同方面</strong>：<br>　　■ 以<em>delegate</em>关键字开头<br>　　■ 没有方法主体</p>
<h3 id="创建委托对象"><a href="#创建委托对象" class="headerlink" title="创建委托对象"></a>创建委托对象</h3><p>　　<strong>委托是引用类型</strong>，因此有引用和对象。我们可以声明变量并创建类型的对象。<br><img src="/image/cSharp/cSharp34.png" alt=""></p>
<p>　　有两种创建委托对象的方式：<br>　　第一种是使用带<em>new</em>运算符的对象创建表达式。<br><img src="/image/cSharp/cSharp35.png" alt=""><br>　　第二种是使用快捷语法，直接使用方法赋值。使用快捷语法方法名称和其相对应的委托类型之间有隐式转换。<br><img src="/image/cSharp/cSharp36.png" alt=""><br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">delegate</span> <span class="keyword">void</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x</span>)</span>;       <span class="comment">//声明委托类型</span></span><br><span class="line">MyDel delVar,dVar;                <span class="comment">//创建两个委托变量</span></span><br><span class="line"></span><br><span class="line">delVar= <span class="keyword">new</span> MyDel(myInstObj.MyM1);<span class="comment">//创建委托并保存引用</span></span><br><span class="line">dVar =<span class="keyword">new</span> MyDel(SClass.OtherM2);  <span class="comment">//创建委托并保存引用</span></span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp37.png" alt=""><br>当然也可以这样创建：<br><figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">MyDel delVar</span>=new MyDel(myInstObj.MyM1);</span><br><span class="line"><span class="attribute">MyDel dVar</span>= new MyDel(SClass.OtherM2);</span><br></pre></td></tr></table></figure></p>
<p>或者使用快捷语法：<br><figure class="highlight ebnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">MyDel delVar</span>=myInstObj.MyM1;</span><br><span class="line"><span class="attribute">MyDel dVar</span>= SClass.OtherM2;</span><br></pre></td></tr></table></figure></p>
<h3 id="委托赋值"><a href="#委托赋值" class="headerlink" title="委托赋值"></a>委托赋值</h3><p>　　由于委托是引用类型，我们可以通过给它赋值来改变包含在委托变量中的引用，旧的委托对象会被GC回收。<br><figure class="highlight abnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">MyDel delVar<span class="comment">;</span></span><br><span class="line"><span class="attribute">delVar</span> = myInstObj.MyM1<span class="comment">;</span></span><br><span class="line">//重新赋值</span><br><span class="line"><span class="attribute">delVar</span> = SClass.OtherM2<span class="comment">;</span></span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp38.png" alt=""></p>
<h3 id="组合委托"><a href="#组合委托" class="headerlink" title="组合委托"></a>组合委托</h3><p>　　上面描述的委托的调用列表中都只有一个方法，委托可以使用<strong>+</strong>运算符来“组合”。这个运算最终会创建一个新的委托，新委托的调用列表是两个被操作委托调用列表的组合。<br>　　<strong>组合委托</strong>的操作数委托没有被修改，当组合委托的操作发生时，参与运算的委托都没有发生改变，只是创建了一个新的委托。<strong>委托是恒定的，委托对象被创建后就不会再被改变。</strong><br><figure class="highlight protobuf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">MyDel delA = myInstObj.MyM1;</span><br><span class="line">MyDel delB = SClass.OtherM2;</span><br><span class="line"></span><br><span class="line">MyDel delC = delA + delB;    <span class="comment">//组合委托会组合操作数委托的调用列表</span></span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp39.png" alt=""></p>
<h3 id="为委托增加和移除方法"><a href="#为委托增加和移除方法" class="headerlink" title="为委托增加和移除方法"></a>为委托增加和移除方法</h3><p>　　<strong>增加方法</strong>：<br>　　虽然委托是不可变的，但是c#提供了看上去可以为委托增加方法的语法，我们可以通过使用“<strong>+=</strong>”运算符来为委托增加方法或者另一个委托。<br><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MyDel delVar = inst.MyM1;    //创建委托并初始化</span><br><span class="line">delVar += SCl.m3;            //增加方法</span><br><span class="line">delVar += X.Act;             //增加方法</span><br></pre></td></tr></table></figure></p>
<p>　　<strong>在使用+=运算符时，实际上是创建了个新的委托，新委托的调用列表是左边的委托加上右边要添加的方法。</strong><br><img src="/image/cSharp/cSharp40.png" alt=""></p>
<p>　　<strong>移除方法</strong>：<br>　　可以使用<strong>-=</strong>运算符从委托移除方法。<br><code>delVar -= SCl.m3;    //从委托移除方法</code><br>　　与为委托增加方法一样，移除方法其实就是创建了一个新的委托。新的委托是就委托的副本——只是没有了已经移除方法的引用。<br>　　移除委托时需要注意：<br>　　■ 如果在调用列表中的方法有多个实例，<strong>-=</strong>运算符将从列表最后还是搜索，并且移除第一个与方法匹配的实例。<br>　　■ 试图删除委托中不存在的方法没有效果。<br>　　■ 试图调用空委托会抛出异常。<br>　　■ 我们可以通过把委托和null进行比较来判断委托的调用列表是否为空。<strong>如果调用列表为空，则委托是null</strong>。  </p>
<h3 id="调用委托"><a href="#调用委托" class="headerlink" title="调用委托"></a>调用委托</h3><p>　　可以向调用方法一样简单的调用委托。<br><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">delegate void MyDel (int x);</span><br><span class="line">MyDel delVar =inst.MyM1;</span><br><span class="line">delVar += SCl.m3;</span><br><span class="line">delVar += X.Act;</span><br><span class="line">   ....</span><br><span class="line">delVar(<span class="number">55</span>);    <span class="comment">//调用委托</span></span><br><span class="line">   ....</span><br></pre></td></tr></table></figure></p>
<p><img src="/image/cSharp/cSharp41.png" alt=""><br>　　当委托被调用时，它使用被调用时的相同参数来调用调用列表中的每一个方法。<br>　　如果在调用列表里有好多个相同的方法，当委托被调用时，每次在列表中遇到这个方法时都会调用一次。
　　</p>
<p><strong>调用不带返回值的委托示例</strong>：<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Delegate</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//定义一个没有参数和返回值的委托</span></span><br><span class="line">    <span class="function"><span class="keyword">delegate</span> <span class="keyword">void</span> <span class="title">PrintFunction</span>(<span class="params"></span>)</span>;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            Test t = <span class="keyword">new</span> Test();</span><br><span class="line">            PrintFunction pf; <span class="comment">//创建一个空委托</span></span><br><span class="line">            pf = t.Print1;    <span class="comment">//初始化该委托</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//给委托增加方法</span></span><br><span class="line">            pf += Test.Print2;</span><br><span class="line">            pf += t.Print1;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//调用委托</span></span><br><span class="line">            <span class="keyword">if</span> (pf != <span class="literal">null</span>)</span><br><span class="line">                pf();</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">                Console.WriteLine(<span class="string">"该委托为空！"</span>);</span><br><span class="line">            Console.ReadKey();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Test</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Print1</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            Console.WriteLine(<span class="string">"Print1--实例方法"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Print2</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            Console.WriteLine(<span class="string">"Print2--静态方法"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><img src="/image/cSharp/cSharp42.png" alt=""><br>　　在上面的代码中，我们给委托增加了三个方法，其中有两个方法(Print1)是重复的，从运行结果可以看出，按照顺序，<code>Print1</code>被调用了2次。<br>　　<strong>调用委托其实就是调用了委托的调用列表中的所有方法。</strong></p>
<p><strong>调用带返回值的委托示例</strong>：<br>　　如果委托有返回值，并且在调用列表中有一个以上的方法存在，那么：<br>　　■ 调用列表中的最后一个方法的返回值就是委托调用返回的值。<br>　　■ 调用列表中所有其他方法的返回值都会被忽略。<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Delegate2</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//创建一个具有返回值，没有参数的委托</span></span><br><span class="line">    <span class="function"><span class="keyword">delegate</span> <span class="keyword">int</span> <span class="title">MyDel</span>(<span class="params"></span>)</span>;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            Test t = <span class="keyword">new</span> Test();</span><br><span class="line">            MyDel mDel = t.Add2;</span><br><span class="line">            mDel += t.Add3;</span><br><span class="line">            mDel += t.Add2;</span><br><span class="line">            Console.WriteLine(<span class="string">"值为：&#123;0&#125;"</span>,mDel());</span><br><span class="line">            Console.ReadKey();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Test</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">int</span> <span class="keyword">value</span> = <span class="number">5</span>;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">Add2</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">value</span> += <span class="number">2</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">value</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">Add3</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">value</span> += <span class="number">3</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">value</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><img src="/image/cSharp/cSharp43.png" alt=""><br>　　■ 上面代码中的委托添加了三个方法，分别是<code>Add2</code>、<code>Add3</code>、<code>Add2</code>。<br>　　■ 在最后打印的时候调用了委托，执行的过程是：<img src="/image/cSharp/cSharp44.png" alt=""></p>
<p><strong>调用带引用参数的委托示例</strong><br>　　如果委托有引用参数，在调用委托列表中的下一个方法时，参数的新值(不是初始值)会传给下一个方法。以此类推。<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Delegate3</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">delegate</span> <span class="keyword">void</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">ref</span> <span class="keyword">int</span> x</span>)</span>;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            Test t = <span class="keyword">new</span> Test();</span><br><span class="line">            MyDel mDel = t.Add2;</span><br><span class="line">            mDel += t.Add3;</span><br><span class="line">            mDel += t.Add2;</span><br><span class="line">            <span class="keyword">int</span> x = <span class="number">5</span>;</span><br><span class="line">            mDel(<span class="keyword">ref</span> x);</span><br><span class="line">            Console.WriteLine(<span class="string">"值为：&#123;0&#125;"</span>,x);</span><br><span class="line">            Console.ReadKey();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Test</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Add2</span>(<span class="params"><span class="keyword">ref</span> <span class="keyword">int</span> x</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            x += <span class="number">2</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Add3</span>(<span class="params"><span class="keyword">ref</span> <span class="keyword">int</span> x</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            x += <span class="number">3</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<br><img src="/image/cSharp/cSharp45.png" alt=""><br>　　■ 引用参数是按顺序在调用方法中一次传递的：<img src="/image/cSharp/cSharp46.png" alt=""></p>
<h3 id="匿名方法"><a href="#匿名方法" class="headerlink" title="匿名方法"></a>匿名方法</h3><p>　　如果一个方法在调用列表中，或者说在委托的调用中只会被使用一次，那么则没有必要创建一个独立的具名方法，可以使用匿名方法。<br>　　<strong>匿名方法</strong>(anonymous metfod)是在初始化委托是内联声明的方法。</p>
<p><strong>具名方法代码示例：</strong><br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Delegate4</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">delegate</span> <span class="keyword">int</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x</span>)</span>;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            MyDel md = Add;</span><br><span class="line"></span><br><span class="line">            Console.WriteLine(<span class="string">"具名方法的输出：&#123;0&#125;"</span>,md(<span class="number">5</span>));</span><br><span class="line">            Console.WriteLine(<span class="string">"具名方法的输出：&#123;0&#125;"</span>,md(<span class="number">6</span>));</span><br><span class="line">            Console.ReadKey();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">Add</span>(<span class="params"><span class="keyword">int</span> x</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> x += <span class="number">20</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<img src="/image/cSharp/cSharp47.png" alt=""><br><strong>匿名方法代码示例：</strong><br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">Delegate5</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">delegate</span> <span class="keyword">int</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x</span>)</span>;</span><br><span class="line">    <span class="keyword">class</span> <span class="title">Program</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            MyDel md = <span class="keyword">delegate</span> (<span class="keyword">int</span> x)</span><br><span class="line">              &#123;</span><br><span class="line">                  <span class="keyword">return</span> x += <span class="number">20</span>;</span><br><span class="line">              &#125;;</span><br><span class="line">            Console.WriteLine(<span class="string">"匿名方法的输出：&#123;0&#125;"</span>,md(<span class="number">5</span>));</span><br><span class="line">            Console.WriteLine(<span class="string">"匿名方法的输出：&#123;0&#125;"</span>,md(<span class="number">6</span>));</span><br><span class="line">            Console.ReadKey();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：<img src="/image/cSharp/cSharp48.png" alt=""><br>　　可以看到两种方式都得到了相同的计算结果。<strong>一个方法只会被使用一次，这个方法初始化委托时可以使用匿名方法的形式。</strong></p>
<h4 id="在什么地方使用匿名方法："><a href="#在什么地方使用匿名方法：" class="headerlink" title="在什么地方使用匿名方法："></a>在什么地方使用匿名方法：</h4><p>　　■ 声明委托变量时作为初始化表达式<br>　　■ 组合委托是在赋值语句的右边<br>　　■ 为委托增加事件时在赋值语句的右边</p>
<h4 id="匿名方法的语法："><a href="#匿名方法的语法：" class="headerlink" title="匿名方法的语法："></a>匿名方法的语法：</h4><p><img src="/image/cSharp/cSharp49.png" alt=""><br>　　■ <strong>delegate</strong>类型关键字。<br>　　■ 参数列表，如果语句块没有使用任何参数则可以省略。<br>　　■ 语句块，它包含了匿名方法的代码。
　　</p>
<h5 id="1、返回类型："><a href="#1、返回类型：" class="headerlink" title="1、返回类型："></a>1、返回类型：</h5><p>　　<strong>匿名方法不会显示的声明返回值</strong>。但是匿名方法的实现本身的行为必须一个类型上与委托返回值类型想匹配的值。<strong>如果委托有void类型的返回值，那么匿名方法就不能有返回值</strong>。<br>例如：<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">delegate</span> <span class="keyword">int</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x</span>)</span>;     <span class="comment">//委托类型的返回值类型是int</span></span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">Main</span>(<span class="params"><span class="keyword">string</span>[] args</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    MuDel md = <span class="keyword">delegate</span> (<span class="keyword">int</span> x)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">return</span> x + <span class="number">20</span>; <span class="comment">//匿名方法也必须返回一个int类型的值</span></span><br><span class="line">            &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h5 id="2、参数"><a href="#2、参数" class="headerlink" title="2、参数"></a>2、参数</h5><p>　　除了参数是数组的情况，匿名方法的参数列表必须在三个方面匹配委托：<br>　　■ 参数数量<br>　　■ 参数类型<br>　　■ 修饰符<br>　　<br>　　<strong>我们可以在委托的参数列表不包含任何out参数，并且匿名方法不使用任何参数的情况下，通过圆括号为空或者直接省略圆括号来简化匿名方法的参数列表。</strong><br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">delegate</span> <span class="keyword">void</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x</span>)</span>; </span><br><span class="line"></span><br><span class="line"><span class="comment">//调用委托</span></span><br><span class="line">MyDel md = <span class="keyword">delegate</span>      <span class="comment">//省略的参数列表</span></span><br><span class="line">        &#123;</span><br><span class="line">            Console.WriteLine(<span class="string">"简化匿名方法！"</span>);  </span><br><span class="line">        &#125;;</span><br></pre></td></tr></table></figure></p>
<h5 id="3、params参数"><a href="#3、params参数" class="headerlink" title="3、params参数"></a>3、params参数</h5><p>　　如果委托声明的参数列表中包含了params参数，那么使用匿名方法时，匿名方法的参数列表就会忽略params关键字。<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">delegate</span> <span class="keyword">void</span> <span class="title">MyDel</span>(<span class="params"><span class="keyword">int</span> x,<span class="keyword">params</span> <span class="keyword">int</span>[] y</span>)</span>;   <span class="comment">//在委托类型的声明中使用了params关键字</span></span><br><span class="line"></span><br><span class="line">MyDel md = <span class="keyword">delegate</span>(<span class="keyword">int</span> x,<span class="keyword">int</span>[] y)    <span class="comment">//在匹配的匿名方法中忽略了params关键字</span></span><br><span class="line">        &#123;</span><br><span class="line">           ......</span><br><span class="line">        &#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="变量和参数的作用域"><a href="#变量和参数的作用域" class="headerlink" title="变量和参数的作用域"></a>变量和参数的作用域</h3><p>　　匿名方法的参数以及声明在匿名方法内部的局部变量的作用域限制在匿名方法的主体之内。<br><img src="/image/cSharp/cSharp50.png" alt=""></p>
<h5 id="1、外部变量"><a href="#1、外部变量" class="headerlink" title="1、外部变量"></a>1、外部变量</h5><p>　　匿名方法可以访问它的外围作用域的局部变量和环境。<br>　　■ 外围作用域的变量叫做<strong>外部变量</strong>(outer variable)。<br>　　■ 用在匿名方法实现代码中的外部变量称为<strong>被方法捕获</strong>(captured)。<br><img src="/image/cSharp/cSharp51.png" alt=""></p>
<h5 id="2、被方法捕获变量的生命周期扩展"><a href="#2、被方法捕获变量的生命周期扩展" class="headerlink" title="2、被方法捕获变量的生命周期扩展"></a>2、被方法捕获变量的生命周期扩展</h5><p><img src="/image/cSharp/cSharp52.png" alt=""><br>　　■ 局部变量x在块中声明和初始化<br>　　■ 委托mDel被匿名方法初始化，该匿名方法捕获了外部变量x<br>　　■ 当x离开块区域时，引用x的值会发生编译错误<br>　　■ 但是，委托mDel中的匿名方法在它的环境中保留了变量x，并且在mDel被调用时可以输出x的值。</p>
<h2 id="Lambda表达式"><a href="#Lambda表达式" class="headerlink" title="Lambda表达式"></a>Lambda表达式</h2><p>　　c#2.0中引入了匿名方法，它允许我们在创建或者为委托增加方法时包含小段内联代码。<br>　　c#3.0中引入了lambda表达式，简化了匿名方法的语法。<br>　　<br>　　在匿名方法的语法中，delegate关键字是有点多余的，因为编译器已经知道我们在将方法赋值给委托，我们可以把匿名方法转换为lambda表达式。<br>　　■ 删除delegate关键字<br>　　■ 在参数列表和匿名方法主体之间放lambda运算符“<strong>=&gt;</strong>”<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">MyDel md = <span class="keyword">delegate</span>(<span class="keyword">int</span> x)    &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//匿名方法</span></span><br><span class="line">MyDel md =         (<span class="keyword">int</span> x) =&gt; &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//lambda表达式</span></span><br></pre></td></tr></table></figure></p>
<p>　　编译器通过推断允许我们更进一步简化lambda表达式：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">MyDel md  = delegate(int x)    &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//匿名方法</span></span><br><span class="line">MyDel md2 =         <span class="function">(<span class="params">int x</span>) =&gt;</span> &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//lambda表达式</span></span><br><span class="line">MyDel md3 =             <span class="function">(<span class="params">x</span>) =&gt;</span> &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//lambda表达式</span></span><br><span class="line">MyDel md4 =              <span class="function"><span class="params">x</span>  =&gt;</span> &#123;<span class="keyword">return</span> x + <span class="number">1</span>&#125;;   <span class="comment">//lambda表达式</span></span><br><span class="line">MyDel md5 =              <span class="function"><span class="params">x</span>  =&gt;</span>         x + <span class="number">1</span> ;   <span class="comment">//lambda表达式</span></span><br></pre></td></tr></table></figure></p>
<p>　　■ 编译器还可以从委托的声明中知道委托参数的类型，因此lambda表达式允许我们省略类型参数，如上面代码中<code>md3</code>的赋值代码所示。<br>　　　　□ 带有类型的参数列表称为显式类型<br>　　　　□ 省略类型的参数列表称为隐式类型<br>　　■ 如果只有一个隐式类型的参数，我们可以省略周围的圆括号，如上面代码中的<code>md4</code>赋值代码所示。<br>　　■ <strong>lambda表达式允许表达式的主体是语句块或表达式</strong>。如果语句块包含了一个返回语句(return)，我们可以将语句块中的return关键字省略，直接写表达式。如<code>md5</code>的赋值代码所示。</p>
<p>　　有关lambda表达式的参数列表的要点如下：<br>　　■ lambda表达式参数列表中的参数必须在参数数量、类型和位置上与委托相匹配。<br>　　■ 表达式的参数列表中的参数不一定需要包含类型(比如隐式类型)，除非委托有<code>ref</code>或者<code>out</code>参数，此时声明参数类型是必须的(比如显式类型)。<br>　　■ 如果只有一个参数，并且是隐式类型，周围的圆括号可以被省略，否则圆括号就是必须写上的。<br>　　■ 如果没有参数，则必须使用一组空的圆括号代替。</p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/c/">c#</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/c-学习笔记/">c#学习笔记</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2015/05/21/c#-Event/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          事件(Event)
        
      </div>
    </a>
  
  
    <a href="/2015/05/19/Exceptions-try-statements/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">异常(try语句)</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#什么是委托"><span class="nav-number">1.</span> <span class="nav-text">什么是委托</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#声明委托类型"><span class="nav-number">2.</span> <span class="nav-text">声明委托类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建委托对象"><span class="nav-number">3.</span> <span class="nav-text">创建委托对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#委托赋值"><span class="nav-number">4.</span> <span class="nav-text">委托赋值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#组合委托"><span class="nav-number">5.</span> <span class="nav-text">组合委托</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#为委托增加和移除方法"><span class="nav-number">6.</span> <span class="nav-text">为委托增加和移除方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#调用委托"><span class="nav-number">7.</span> <span class="nav-text">调用委托</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#匿名方法"><span class="nav-number">8.</span> <span class="nav-text">匿名方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#在什么地方使用匿名方法："><span class="nav-number">8.1.</span> <span class="nav-text">在什么地方使用匿名方法：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#匿名方法的语法："><span class="nav-number">8.2.</span> <span class="nav-text">匿名方法的语法：</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1、返回类型："><span class="nav-number">8.2.1.</span> <span class="nav-text">1、返回类型：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2、参数"><span class="nav-number">8.2.2.</span> <span class="nav-text">2、参数</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3、params参数"><span class="nav-number">8.2.3.</span> <span class="nav-text">3、params参数</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#变量和参数的作用域"><span class="nav-number">9.</span> <span class="nav-text">变量和参数的作用域</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1、外部变量"><span class="nav-number">9.0.1.</span> <span class="nav-text">1、外部变量</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2、被方法捕获变量的生命周期扩展"><span class="nav-number">9.0.2.</span> <span class="nav-text">2、被方法捕获变量的生命周期扩展</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Lambda表达式"><span class="nav-number"></span> <span class="nav-text">Lambda表达式</span></a>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
